﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Model;

namespace DataAccess
{
    public class SSDDEntities : DbContext
    {
        private static SSDDEntities instance;
        public SSDDEntities()
            : base(System.Configuration.ConfigurationSettings.AppSettings["BranchName"] + "SSDDDB")
        {
            if (!Database.Exists())
            {
                Database.CreateIfNotExists();
                Products.Add(new Product { ProductId = "Kit001", Name = "Kit 109 piezas talad y ator a7064xj", Quality = 0, Trademark = "Ortiz" });
                Products.Add(new Product { ProductId = "Elt001", Name = "Aspirador recogetodo 4,8v bco verde", Quality = 0, Trademark = "Vengala" });
                Products.Add(new Product { ProductId = "Cer001", Name = "Bombillo ips 30x30 niquel llaves", Quality = 0, Trademark = "WizWiz" });
                Products.Add(new Product { ProductId = "Cer002", Name = "Macolla partida m:271.16 tubo 20 ", Quality = 0, Trademark = "Tool" });
                Products.Add(new Product { ProductId = "Aut001", Name = "Aceitera mod.k 200 flex umeta ", Quality = 0, Trademark = "Ortiz" });
                Products.Add(new Product { ProductId = "Jar001", Name = "Cultivador gama aluminio bello  ", Quality = 0, Trademark = "Ortiz" });
                ProductStores.Add(new ProductStore() { ProductId = "Cer001", StoreEnterDate = DateTime.Now, StorePriceByUnit = 35, StoreQuantity = 4 });
                ProductStores.Add(new ProductStore() { ProductId = "Cer002", StoreEnterDate = DateTime.Now, StorePriceByUnit = 20, StoreQuantity = 3 });
                ProductStores.Add(new ProductStore() { ProductId = "Kit001", StoreEnterDate = DateTime.Now, StorePriceByUnit = 200, StoreQuantity = 4 });
                ProductStores.Add(new ProductStore() { ProductId = "Elt001", StoreEnterDate = DateTime.Now, StorePriceByUnit = 50, StoreQuantity = 6 });
                ProductStores.Add(new ProductStore() { ProductId = "Jar001", StoreEnterDate = DateTime.Now, StorePriceByUnit = 10, StoreQuantity = 11 });
                ProductStores.Add(new ProductStore() { ProductId = "Aut001", StoreEnterDate = DateTime.Now, StorePriceByUnit = 15, StoreQuantity = 0 });
                SaveChanges();
            }
        }

        public static SSDDEntities Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SSDDEntities();
                }
                return instance;
            }
        }

        public override int SaveChanges()
        {
            int result = base.SaveChanges();
            instance = new SSDDEntities();
            return result;
        }

        public void DiscardChanges()
        {
            instance = new SSDDEntities();
        }


        public DbSet<Product> Products { get; set; }
        public DbSet<Client> Clients { get; set; }
        public DbSet<Employ> Employs { get; set; }
        public DbSet<ProductStore> ProductStores { get; set; }
        public DbSet<Depot> Depots { get; set; }
        public DbSet<Branch> Branch { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Sale> Sales { get; set; }
        public DbSet<SaleDetail> SaleDetails { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
    }
}
